//
//  WelcomeViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/16.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#import "WelcomeViewController.h"
#import "SignViewController.h"
@interface WelcomeViewController ()

@end

@implementation WelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化scrollView
    [self _initScrollView];
    
    //初始化图片
    [self _initScrollViewImage];
    
    //初始化手势
    [self _initSingleTapGesture];
    
}
//初始化scrollView
- (void)_initScrollView {
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    //设置滚动的尺寸
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
    
    _scrollView.tag = 106;
    
    //不显示水平滚动条
    _scrollView.showsHorizontalScrollIndicator = NO;
    //不允许回弹
    _scrollView.bounces = NO;
    //允许整页滚动
    _scrollView.pagingEnabled = YES;
    //设置代理
    _scrollView.delegate = self;

}

//设置图片
- (void)_initScrollViewImage {
    //在UIScrollView 上加入 UIImageView
    for (int i = 0 ; i < 4; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i , 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        
        //将要加载的图片放入imageView 中
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",i+1]];
        imageView.image = image;
        
        [_scrollView addSubview:imageView];
    }
    
    //初始化 UIPageControl 和 _scrollView 显示在 同一个页面中
//    UIPageControl *pageConteol = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth-50)/2, self.view.frame.size.height - 60, 50, 40)];
//    pageConteol.numberOfPages = 5;//设置pageConteol 的page 和 _scrollView 上的图片一样多
//    //    pageConteol.pageIndicatorTintColor = [UIColor blackColor];
//    pageConteol.tag = 107;
    
    [self.view addSubview:_scrollView];
//    [self.view addSubview: pageConteol];

}

//初始化手势
- (void)_initSingleTapGesture {
    _singleTapGesture = [[UITapGestureRecognizer alloc] init];
    //设置点击
    _singleTapGesture.numberOfTouchesRequired = 1;
    _singleTapGesture.numberOfTapsRequired = 1;
    [_singleTapGesture addTarget:self action:@selector(singleTap:)];
    
    [_scrollView addGestureRecognizer:_singleTapGesture];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 记录scrollView 的当前位置，因为已经设置了分页效果，所以：位置/屏幕大小 = 第几页
    int current = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    
    //根据scrollView 的位置对page 的当前页赋值
    UIPageControl *page = (UIPageControl *)[self.view viewWithTag:107];
    page.currentPage = current;
    
    //    //当显示到最后一页时，让滑动图消失
    //    if (page.currentPage == 3) {
    //
    //        //调用方法，使滑动图消失
    //        [self scrollViewDisappear];
    //    }
}

#pragma mark ---- scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}

#pragma mark ---- 单击手势
- (void)singleTap:(UITapGestureRecognizer *)sender {
    if (_scrollView.contentOffset.x >= kScreenWidth * 2) {
        
        //创建登录界面
        SignViewController * donghua = [[SignViewController alloc] init];
        UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:donghua];
        
        //改变根视图
        [UIApplication sharedApplication].delegate.window.rootViewController = rootNav;
        
    }
    
}

@end
